import axios from "axios"
import { Indicator } from 'mint-ui';
import store from "@/store/index";
import router from "@/router"

// axios.defaults.baseURL = 'http://1.117.248.51:3005';
axios.defaults.baseURL = 'http://127.0.0.1:3005';
export default {

    common: {
        method: 'get',
        data: {},
        params: {},
        headers: {}
    },

    $axios(options = {}) {
        options.method = options.method || this.common.method;
        options.data = options.data || this.common.method;
        options.params = options.params || this.common.params;
        options.headers = options.headers || this.common.headers;
        //请求前==》显示加载中...
        Indicator.open('加载中...');
        
        // 是否需要token
        if (options.headers.token) {
            options.headers.token =store.state.user.token;
            // console.log(options.headers);
            // 没值代表没有登录  没登陆则跳转到登录页
            if(!options.headers.token){
                router.push({
                    name:'Login'
                })
            }
        }
        // 返回数据
        return axios(options).then(v => {
            let data = v.data;
            return new Promise((res, rej) => {
                if (!v) { return rej() }
                setTimeout(() => {
                    Indicator.close('加载中...');
                }, 300);
                return res(data)
            })
        })
    }
}